package com.yozinga.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class YozingaDatabase extends SQLiteOpenHelper{

	public static final String DATABASE = "yozinga.db";
	private static final int VERSION_CODE = 1;
	
	public YozingaDatabase(Context context) {
		super(context, DATABASE, null, VERSION_CODE);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createTables(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion < newVersion) {
			dropTables(db);
			createTables(db);
		}
	}

	public void clearDatabase(SQLiteDatabase db) {
		dropTables(db);
		createTables(db);
	}

	private static final String SQL_CREATE_FAVOURITE_TABLE = "CREATE TABLE "
			+ yozingaDataBaseManager.Favourites.TABLE_FAVOURITE + " ("
			+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_FULL_ADDRESS + " TEXT,"	
			+ yozingaDataBaseManager.Favourites.COL_OFFERID + " TEXT NOT NULL,"	
			+ yozingaDataBaseManager.Favourites.COL_CATEGORYID + " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANTID + " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANTNAME + " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_TAGLINE + " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_LATITUDE+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_LONGITUDE+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_URL+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_VIDEO_LINK+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_PHONE_NUMBER+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_INFO+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_FACEBOOL_URL+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_TWITTER_URL+ " TEXT,"
			+ yozingaDataBaseManager.Favourites.COL_MERCHANT_DRIPPLE_URL+ " TEXT,"
			+ "UNIQUE (" + yozingaDataBaseManager.Favourites.COL_OFFERID	+ ") ON CONFLICT REPLACE)";
	
	private static void dropTables(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + yozingaDataBaseManager.Favourites.TABLE_FAVOURITE);
		
	}

	private static void createTables(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_FAVOURITE_TABLE);
	}
}
